<?php 
namespace App\Models;
use Log;
use App\Cache\BaseCache;

class Live extends BaseModel{
    
	//表明
	protected $table = 'live';
	//主键
	protected $primaryKey = 'id';
	
    //直播实体信息
    public function getLiveObject($id,$liveModel = null){
        $live =$this->getLiveModel((int)$id,$liveModel);
        if(isset($live->id) && $live->is_delete != 1){
//             belong_to
            $liveObject = array(); 
            $liveObject['liveId'] = $live->id;
            $liveObject['title'] = $live->title;
            $liveObject['livePoster'] = $live->live_poster;
            $liveObject['description'] = $live->description;
            $liveObject['keywords'] = $live->keywords;
            $liveObject['status'] = $live->status;
            $liveObject['title'] = $live->title;
            $liveObject['pushStream'] = $live->push_stream;
            $liveObject['pullStream'] = $live->pull_stream;
            $liveObject['liveReview'] = array();
            if($live->live_review){
                $video_arr = json_decode($live->live_review,TRUE);
                if(is_array($video_arr)){
                    foreach ($video_arr as $key=>$video){
                        $liveObject['liveReview'][]= array(
                            'avType'=>'video',
                            'avKey'=>$video,
                            'avWidth'=>100,
                            'avHeight'=>100,
                            'avSize'=>110,
                            'avDuration'=>59,
                            'avPoster'=>'',
                        );
                        $liveObject['shareLink'] = $video;
                    } 
                } else {
                    $liveObject['liveReview'][]= array(
                            'avType'=>'video',
                            'avKey'=>$live->live_review,
                            'avWidth'=>100,
                            'avHeight'=>100,
                            'avSize'=>110,
                            'avDuration'=>59,
                            'avPoster'=>'',
                        );
                    $liveObject['shareLink'] = $live->live_review;
                }
            }
            $liveObject['belongTo'] = $live->belong_to;
            $liveObject['bookStatus'] = 0; //是否预定 
            $liveObject['favoriteStatus'] = 0; //是否收藏
            if($this->request->input('loginStatus') == 1){
                $cache = new BaseCache();
                $cache->method = 'get';
                $cache->cacheName = 'favorite';
                $cache->cachePar = array($this->request->input('userId'),$live->id); //集合
                $retcache = $cache->HandleCache();
                if($retcache == 'NO_CACHE'){
                   $status = with(new Favorite($this->request))->isFavorite($this->request->input('userId'),$live->id);
                   if($status){
                       $liveObject['favoriteStatus'] = 1;
                   }
                }else if($retcache == 'FAVORITE'){
                    $liveObject['favoriteStatus'] = 1;
                }
                
                $cache->method = 'get';
                $cache->cacheName = 'book';
                $cache->cachePar = array($this->request->input('userId'),$live->id); //集合
                $retcache = $cache->HandleCache();
                if($retcache == 'NO_CACHE'){
                   $status = with(new BookRecord($this->request))->isBook($this->request->input('userId'),$live->id);
                   if($status){
                       $liveObject['bookStatus'] = 1;
                   }
                }else if($retcache == 'BOOK'){
                    $liveObject['bookStatus'] = 1;
                }
                
            }
            $liveObject['startTime'] = $live->start_time;
            $liveObject['visitTotal'] = $live->visit_total + $live->set_visit_num;
            $liveObject['prasieTotal'] = $live->praise_total+ $live->set_praise_num;
            $liveObject['anchor'] = with(new User($this->request))->getUserObejct((int)$live->anchor_id);
            $liveObject['source'] = with(new Organization($this->request))->getOrganizationObejct($live->source_id);
            $liveObject['city'] = $live->city;
            return $liveObject; 
        }
        return ;
    }
    
    //获取直播
    public function getLiveModel($id,$liveModel = null){
        $liveModelCache = new BaseCache();
        $liveModelCache->method = 'get';
        $liveModelCache->cacheName = 'liveModel';
        $liveModelCache->cachePar = array($id); //集合
        $retCache = $liveModelCache->HandleCache();
        if ($retCache !== 'NO_CACHE') {
            if(!isset($retCache->id)){
                return;
            }
            return $retCache;
        }
        if($liveModel){
            $live = $liveModel;
        } else if($id > 0){
            $live = Live::select('live.id', 'live.title', 'live.description', 'live.live_poster', 'live.keywords', 'live.status', 
                    'live.push_stream', 'live.pull_stream', 'live.live_review', 'live.start_time', 'live.visit_total','live.praise_total','live.order_total',
                    'live.set_praise_num','live.set_visit_num','live.anchor_id','live.source_id','live.belong_to','city','live.comment_enable','live.res_id','live.is_delete')
                    ->where('live.id','=' ,$id)
                    ->first();
        } else {
            $live = 'CACHE_BREAKDOWN';
        }
        $liveModelCache->method = 'put';
        $liveModelCache->cacheValue = $live;
        $liveModelCache->HandleCache();
        if(!isset($live->id)){
            return;
        }
        return $live;
    }
    
    public function getLiveList(){
        $categoryId = $this->request->input('categoryId');
        $page = $this->request->input('page');
        $size = $this->request->input('size');
        //取缓存
        $liveListCache = new BaseCache();
        $liveListCache->method = 'get';
        $liveListCache->cacheName = 'liveList';
        $liveListCache->cachePar = array($categoryId); //集合
        $retliveListCache = $liveListCache->HandleCache();
        if ($retliveListCache === 'NO_CACHE' || ( isset($retliveListCache['more']) && $retliveListCache['more'] == 1 && count($retliveListCache['data']) <= ($info['pageSize'] * ($info['page'] + 1)) )) {
            // 1、不存在缓存 2、有缓存但缓存数量不够且数据库里仍有多个数据
//          判断页码数目
            $skip = 0;
            if ($retliveListCache !== 'NO_CACHE') {
                $skip = $retliveListCache['num'];
            }
            $take_ready = floor(($size * ($page + 1)) / $this->TAKE_NUM) + 1;
            $take = $take_ready - $skip;
            
            $lives = Live::select('live.id', 'live.title', 'live.description', 'live.live_poster', 'live.keywords', 'live.status', 
                    'live.push_stream', 'live.pull_stream', 'live.live_review', 'live.start_time', 'live.visit_total','live.praise_total','live.order_total',
                    'live.set_praise_num','live.set_visit_num','live.anchor_id','live.source_id','live.belong_to','city','live.comment_enable','live.res_id','live.is_delete')
                    ->where('live.is_delete','=','0')
                    ->where('live.status', '!=', '4')
                    ->join('live_has_category','live_has_category.live_id','=','live.id')
                    ->where('live_has_category.category_id','=',$categoryId)
                    ->where('live_has_category.is_delete','=','0')
                    ->orderBy('live_has_category.sort', 'asc')
                    ->orderBy('live.visit_total', 'desc')
                    ->skip($skip * $this->TAKE_NUM)
                    ->take($take * $this->TAKE_NUM)
                    ->get();
            $retLiveListCacheNew = array();
            foreach ($lives as $key => $live) {
                //存缓存-commentModel
                $liveModelCache = new BaseCache();
                $liveModelCache->method = 'put';
                $liveModelCache->cacheName = 'liveModel';
                $liveModelCache->cachePar = array($live->id); //集合
                $liveModelCache->cacheValue = $live;
                $liveModelCache->HandleCache();
                $retLiveListCacheNew['data'][]['id'] = $live->id;
            }
            if (count($lives) < $take * $this->TAKE_NUM) {
                $retLiveListCacheNew['more'] = 0;
            } else {
                $retLiveListCacheNew['more'] = 1;
            }
            $retLiveListCacheNew['num'] = $skip + ceil(count($lives) / $this->TAKE_NUM);
//            Log::info('$retLiveListCacheNew more' . $retLiveListCacheNew['more']);
//            Log::info('$retLiveListCacheNew $ake_ready' . $take_ready);
//            Log::info('$retLiveListCacheNew $num' . $retLiveListCacheNew['num']);
            if ($retliveListCache !== 'NO_CACHE') {
                $retLiveListCacheNew['data'] = array_merge((array) $retliveListCache['data'], (array) $retLiveListCacheNew['data']);
            }
            $retliveListCache =  $retLiveListCacheNew ;
            //存缓存
            $liveListCache = new BaseCache();
            $liveListCache->method = 'put';
            $liveListCache->cacheName = 'liveList';
            $liveListCache->cachePar = array($categoryId); //集合
            $liveListCache->cacheValue = $retliveListCache;
            $liveListCache->HandleCache();
        }
        
        //遍历缓存数组 
        $index = $size * $page;
        $liveIdList = array();
        for ($i = 0; $i < $size; $i++) {
            if (!isset($retliveListCache['data'][$index])) {
                break;
            }
            $liveIdList[] = $retliveListCache['data'][$index]['id'];
            $index++;
        }
        $data['paging']['size'] = $i;
        
        //判断是否有缓存 实体
        $liveModelCache = new BaseCache();
        $liveModelCache->method = 'has';
        $liveModelCache->cacheName = 'liveModel';
        $index_ids = array();
        foreach ($liveIdList as $key => $value) {
             $liveModelCache->cachePar = array($value); //集合
            if ($liveModelCache->HandleCache() === 'NO_CACHE') {
                if (!$value) {
                    continue;
                }
                $index_ids[] = $value;
            }
        }
        if (count($index_ids) > 0) {
            //搜索
            $lives = Live::select('live.id', 'live.title', 'live.description', 'live.live_poster', 'live.keywords', 'live.status', 
                    'live.push_stream', 'live.pull_stream', 'live.live_review', 'live.start_time', 'live.visit_total','live.praise_total','live.order_total',
                    'live.set_praise_num','live.set_visit_num','live.anchor_id','live.source_id','live.belong_to','city','live.comment_enable','live.res_id','live.is_delete')
                    ->whereIn('live.id', $index_ids)
                    ->where('live.is_delete', '=', '0')
                    ->where('live.status', '!=', '4')
                    ->get();
            foreach ($index_ids as $index_id) {
                $live_id = 0;
                foreach ($lives as $key => $live) {
                    if ($index_id == $live->id) {
                        $live_id = $index_id;
                        break;
                    }
                }
                $liveModelCache = new BaseCache();
                $liveModelCache->method = 'put';
                $liveModelCache->cacheName = 'commentModel';
                $liveModelCache->cachePar = array($live_id); //集合
                if ($live_id == 0) {
                    $liveModelCache->cacheValue = 'CACHE_BREAKDOWN';
                } else {
                    $liveModelCache->cacheValue = $live;
                }
                $liveModelCache->HandleCache();
            }
        }
        $data['data'] = array();
        //找出commentObject
        foreach ($liveIdList as $key => $id) {
            $liveModel = $this->getLiveObject($id);
            if ($liveModel) {
                $data['data'][] = $liveModel;
            }
        }
        return $data;
    }
    
    public function getUserLiveList(){
        $uId = $this->request->input('uId');
        $type = $this->request->input('type');
        $page = $this->request->input('page');
        $size = $this->request->input('size');
        //取缓存
        $liveListCache = new BaseCache();
        $liveListCache->method = 'get';
        $liveListCache->cacheName = 'userLiveList';
        $liveListCache->cachePar = array($type,$uId); //集合
        $retliveListCache = $liveListCache->HandleCache();
        if ($retliveListCache === 'NO_CACHE' || ( isset($retliveListCache['more']) && $retliveListCache['more'] == 1 && count($retliveListCache['data']) <= ($info['pageSize'] * ($info['page'] + 1)) )) {
            // 1、不存在缓存 2、有缓存但缓存数量不够且数据库里仍有多个数据
//          判断页码数目
            $skip = 0;
            if ($retliveListCache !== 'NO_CACHE') {
                $skip = $retliveListCache['num'];
            }
            $take_ready = floor(($size * ($page + 1)) / $this->TAKE_NUM) + 1;
            $take = $take_ready - $skip;
            
            $lives = Live::select('live.id', 'live.title', 'live.description', 'live.live_poster', 'live.keywords', 'live.status', 
                    'live.push_stream', 'live.pull_stream', 'live.live_review', 'live.start_time', 'live.visit_total','live.praise_total','live.order_total',
                    'live.set_praise_num','live.set_visit_num','live.anchor_id','live.source_id','live.belong_to','city','live.comment_enable','live.res_id','live.is_delete');
            if($type == 1){
                $lives = $lives->where('live.anchor_id','=',$uId);
            }else{
                $lives = $lives->where('live.source_id','=',$uId);
            }       
                $lives = $lives->where('live.is_delete','=','0')
                    ->where('live.status','!=','4')
                    ->orderBy('live.created_at', 'desc')
                    ->skip($skip * $this->TAKE_NUM)
                    ->take($take * $this->TAKE_NUM)
                    ->get();
            $retLiveListCacheNew = array();
            foreach ($lives as $key => $live) {
                //存缓存-commentModel
                $liveModelCache = new BaseCache();
                $liveModelCache->method = 'put';
                $liveModelCache->cacheName = 'liveModel';
                $liveModelCache->cachePar = array($live->id); //集合
                $liveModelCache->cacheValue = $live;
                $liveModelCache->HandleCache();
                $retLiveListCacheNew['data'][]['id'] = $live->id;
            }
            if (count($lives) < $take * $this->TAKE_NUM) {
                $retLiveListCacheNew['more'] = 0;
            } else {
                $retLiveListCacheNew['more'] = 1;
            }
            $retLiveListCacheNew['num'] = $skip + ceil(count($lives) / $this->TAKE_NUM);
//            Log::info('$retLiveListCacheNew more' . $retLiveListCacheNew['more']);
//            Log::info('$retLiveListCacheNew $ake_ready' . $take_ready);
//            Log::info('$retLiveListCacheNew $num' . $retLiveListCacheNew['num']);
            if ($retliveListCache !== 'NO_CACHE') {
                $retLiveListCacheNew['data'] = array_merge((array) $retliveListCache['data'], (array) $retLiveListCacheNew['data']);
            }
            $retliveListCache =  $retLiveListCacheNew ;
            //存缓存
            $liveListCache = new BaseCache();
            $liveListCache->method = 'put';
            $liveListCache->cacheName = 'userLiveList';
            $liveListCache->cachePar = array($type,$uId); //集合
            $liveListCache->cacheValue = $retliveListCache;
            $liveListCache->HandleCache();
        }
        
        //遍历缓存数组 
        $index = $size * $page;
        $liveIdList = array();
        for ($i = 0; $i < $size; $i++) {
            if (!isset($retliveListCache['data'][$index])) {
                break;
            }
            $liveIdList[] = $retliveListCache['data'][$index]['id'];
            $index++;
        }
        $data['paging']['size'] = $i;
        //判断是否有缓存 实体
        $liveModelCache = new BaseCache();
        $liveModelCache->method = 'has';
        $liveModelCache->cacheName = 'liveModel';
        $index_ids = array();
        foreach ($liveIdList as $key => $value) {
             $liveModelCache->cachePar = array($value); //集合
            if ($liveModelCache->HandleCache() === 'NO_CACHE') {
                if (!$value) {
                    continue;
                }
                $index_ids[] = $value;
            }
        }
        if (count($index_ids) > 0) {
            //搜索
            $lives = Live::select('live.id', 'live.title', 'live.description', 'live.live_poster', 'live.keywords', 'live.status', 
                    'live.push_stream', 'live.pull_stream', 'live.live_review', 'live.start_time', 'live.visit_total','live.praise_total','live.order_total',
                    'live.set_praise_num','live.set_visit_num','live.anchor_id','live.source_id','live.belong_to','city','live.comment_enable','live.res_id','live.is_delete')
                    ->whereIn('live.id', $index_ids)
                    ->where('live.is_delete', '=', '0')
                    ->where('live.status', '!=', '4')
                    ->get();
            foreach ($index_ids as $index_id) {
                $live_id = 0;
                foreach ($lives as $key => $live) {
                    if ($index_id == $live->id) {
                        $live_id = $index_id;
                        break;
                    }
                }
                $liveModelCache = new BaseCache();
                $liveModelCache->method = 'put';
                $liveModelCache->cacheName = 'liveModel';
                $liveModelCache->cachePar = array($live_id); //集合
                if ($live_id == 0) {
                    $liveModelCache->cacheValue = 'CACHE_BREAKDOWN';
                } else {
                    $liveModelCache->cacheValue = $live;
                }
                $liveModelCache->HandleCache();
            }
        }
        $data['data'] = array();
        //找出commentObject
        foreach ($liveIdList as $key => $id) {
            $liveModel = $this->getLiveObject($id);
            if ($liveModel) {
                $data['data'][] = $liveModel;
            }
        }
        return $data;
    }
    
    //获取直播详情页面
    public function getLiveDetail() {
        $liveId = $this->request->input('liveId');
        if($liveId){
            return $this->getLiveObject($liveId);
        }
        return;
    }
    
    //获取相关推荐
    public function getRecommendLive() {
        $liveId = $this->request->input('liveId');
        $keywords = $this->request->input('keywords');
        $page = $this->request->input('page');
        $size = $this->request->input('size');
        
        $liveListCache = new BaseCache();
        $liveListCache->method = 'get';
        $liveListCache->cacheName = 'recommendLive';
        $liveListCache->cachePar = array($keywords); //集合
        $retliveListCache = $liveListCache->HandleCache();
        if($retliveListCache == 'NO_CACHE' || 1==1){
             $arr_keywords = explode(',', $keywords);
            $where = '1 = 1';
            foreach ($arr_keywords as $key =>$value) {
                if($key == 0){
                    $where = ' live.keywords like "%' .$value.'%"';
                    continue;
                }
                $where .= ' or live.keywords like "%'.$value.'%"';
            }
            $lives = Live::select('live.id', 'live.title', 'live.description', 'live.live_poster', 'live.keywords', 'live.status', 
                        'live.push_stream', 'live.pull_stream', 'live.live_review', 'live.start_time', 'live.visit_total','live.praise_total','live.order_total',
                        'live.set_praise_num','live.set_visit_num','live.anchor_id','live.source_id','live.belong_to','city','live.comment_enable','live.res_id','live.is_delete')
                    ->whereRaw(' 1=1 and ('.$where.')')
                    ->where('is_delete', '=', '0')
                    ->where('status', '!=', '4')
                    ->whereRaw(" 1=1 order by field (live.status,2,1,3),live.visit_total desc")
                    ->take(50)
                    ->get();
            $liveListCache->method = 'put';
            $liveListCache->cacheValue = $lives;
            $liveListCache->HandleCache();
        }else{
            $lives = array();
            if(is_array($retliveListCache)) {
                $lives =$retliveListCache;
            }
        }
        //找出commentObject
        $data['data'] = array();
        foreach ($lives as $key => $live) {
            if(!isset($live->id)){
                continue; 
            }
            if($live->id == $liveId){
                continue; 
            }
            $liveModel = $this->getLiveObject($live->id,$live);
            if ($liveModel) {
                $data['data'][] = $liveModel;
            }
        }
        //遍历缓存数组 
        if($page > 0){
            $data['paging']['size'] = 0;
        } else {
            $data['paging']['size'] = count($lives);
        }
        return $data;
    }
    
    //获取精选轮播
    public function getLunboLive() {
        $lunboCache = new BaseCache();
        $lunboCache->method = 'get';
        $lunboCache->cacheName = 'lunbo';//精选
        $retLunboCache = $lunboCache->HandleCache();
        if(isset($retLunboCache['liveId'])){
            return $retLunboCache;
        }
        $lun_bo_id = (new RecommendCategory($this->request))->JINGXUAN_LUNBO;
        $lives = Live::select('live.id', 'live.title', 'live.description', 'live.live_poster', 'live.keywords', 'live.status', 
                    'live.push_stream', 'live.pull_stream', 'live.live_review', 'live.start_time', 'live.visit_total','live.praise_total','live.order_total',
                    'live.set_praise_num','live.set_visit_num','live.anchor_id','live.source_id','live.belong_to','city','live.comment_enable','live.res_id','live.is_delete')
                    ->where('live.is_delete','=','0')
                    ->where('live.status', '!=', '4')
                    ->join('recommend','recommend.link','=','live.id')
                    ->where('recommend.category_id','=',$lun_bo_id)
                    ->where('recommend.is_delete','=','0')
                    ->orderBy('recommend.sort', 'asc') 
                    ->take(3)
                    ->get();
        $data = array();
        foreach ($lives as $key => $live) {
            if(!isset($live->id)){
                continue; 
            }
            $liveModel = $this->getLiveObject($live->id,$live);
            if ($liveModel) {
                $data[] = $liveModel;
            }
        }
        return $data;
    }
    
    //获取精选首页
    public function getChosen() {
        //取缓存
        $chosenCache = new BaseCache();
        $chosenCache->method = 'get';
        $chosenCache->cacheName = 'chosen';//精选
        $retChosenCache = $chosenCache->HandleCache();
        if(!is_array($retChosenCache) || count($retChosenCache) < 4){
            $chosen = array();
            $chosen[0] =  array('categoryId'=>1,'name'=>'正在直播','categoryType'=>2,'entity'=>$this->getReBo());
            $chosen[1] =  array('categoryId'=>2,'name'=>'直播预告','categoryType'=>3,'entity'=>$this->getYuGao());
            $chosen[2] =  array('categoryId'=>3,'name'=>'排行榜','categoryType'=>4,'entity'=>$this->getPaiHang());
            $chosen[3] =  array('categoryId'=>4,'name'=>'媒体专区','categoryType'=>5,'entity'=> with(new Organization($this->request))->getZhuanQu());
            $chosen[4] =  array('categoryId'=>5,'name'=>'本地同城','categoryType'=>6,'entity'=>$this->getTongCheng());
            $categoryes = with(new Category($this->request))->getCategoryLive();
            foreach ($categoryes as $key => $category) {
                if($category['sort'] >= 10000){
                    continue;
                }
                $this->request->merge(array('page'=>0));
                $this->request->merge(array('size'=>8));
                $this->request->merge(array('categoryId'=>$category['categoryId']));
                $category_arr = $this->getLiveList();
                if(!isset($category_arr['data'])){
                    $category_arr['data'] =array();
                }
                $chosen[] =  array('categoryId'=>$category['categoryId'],'name'=>$category['name'],'categoryType'=>1,'entity'=>$category_arr['data']);
            }
        }else{
            $chosen = $retChosenCache;
        }
       return $chosen;
    }
    
    //正在热播
    public function getReBo() {
       $category_id = (new RecommendCategory($this->request))->LIVE_REBO;
        $lives = Live::select('live.id', 'live.title', 'live.description', 'live.live_poster', 'live.keywords', 'live.status', 
                    'live.push_stream', 'live.pull_stream', 'live.live_review', 'live.start_time', 'live.visit_total','live.praise_total','live.order_total',
                    'live.set_praise_num','live.set_visit_num','live.anchor_id','live.source_id','live.belong_to','city','live.comment_enable','live.res_id','live.is_delete')
                    ->where('live.is_delete','=','0')
                    ->where('live.status', '=', '2')
                    ->join('recommend','recommend.link','=','live.id')
                    ->where('recommend.category_id','=',$category_id)
                    ->where('recommend.is_delete','=','0')
                    ->whereRaw("  1=1 order by field(live.status,2,1,3),recommend.sort desc")
                    ->take(8)
                    ->get();
        $data= array();
        foreach ($lives as $key => $live) {
            if(!isset($live->id)){
                continue; 
            }
            $liveModel = $this->getLiveObject($live->id,$live);
            if ($liveModel) {
                $data[] = $liveModel;
            }
        }
        return $data;
    }
    
    //直播预告
    public function getYuGao() {
        $category_id = (new RecommendCategory($this->request))->LIVE_YUGAO;
        $lives = Live::select('live.id', 'live.title', 'live.description', 'live.live_poster', 'live.keywords', 'live.status', 
                    'live.push_stream', 'live.pull_stream', 'live.live_review', 'live.start_time', 'live.visit_total','live.praise_total','live.order_total',
                    'live.set_praise_num','live.set_visit_num','live.anchor_id','live.source_id','live.belong_to','city','live.comment_enable','live.res_id','live.is_delete')
                    ->where('live.is_delete','=','0')
                    ->where('live.status', '=', '1')
                    ->join('recommend','recommend.link','=','live.id')
                    ->where('recommend.category_id','=',$category_id)
                    ->where('recommend.is_delete','=','0')
                    ->orderBy('recommend.sort', 'asc')
                    ->take(8)
                    ->get();
        $data= array();
        foreach ($lives as $key => $live) {
            if(!isset($live->id)){
                continue; 
            }
            $liveModel = $this->getLiveObject($live->id,$live);
            if ($liveModel) {
                $data[] = $liveModel;
            }
        }
        return $data;
    }
    
    //排行榜
    public function getPaiHang() {
        $lives = Live::select('live.id', 'live.title', 'live.description', 'live.live_poster', 'live.keywords', 'live.status', 
                    'live.push_stream', 'live.pull_stream', 'live.live_review', 'live.start_time', 'live.visit_total','live.praise_total','live.order_total',
                    'live.set_praise_num','live.set_visit_num','live.anchor_id','live.source_id','live.belong_to','city','live.comment_enable','live.res_id','live.is_delete')
                    ->selectRaw('live.set_visit_num+live.visit_total as total')
                    ->where('live.is_delete','=','0')
                    ->where('live.status', '!=', '4')
                    ->where('live.status', '!=', '1')
                    ->whereRaw("  1=1 order by field(live.status,2,1,3),total desc")
                    ->take(7)
                    ->get();
        $data= array();
        foreach ($lives as $key => $live) {
            if(!isset($live->id)){
                continue; 
            }
            $liveModel = $this->getLiveObject($live->id,$live);
            if ($liveModel) {
                $data[] = $liveModel;
            }
        }
        return $data;
    }
    
    //本地同城
    public function getTongCheng() {
        //同城
        $lives = Live::select('live.id', 'live.title', 'live.description', 'live.live_poster', 'live.keywords', 'live.status', 
                    'live.push_stream', 'live.pull_stream', 'live.live_review', 'live.start_time', 'live.visit_total','live.praise_total','live.order_total',
                    'live.set_praise_num','live.set_visit_num','live.anchor_id','live.source_id','live.belong_to','city','live.comment_enable','live.res_id','live.is_delete')
                    ->where('live.is_delete','=','0')
                    ->where('live.status', '!=', '4')
                    ->where('live.city', 'like', '%郑州%')
                    ->whereRaw("  1=1 order by field(live.status,2,1,3),live.visit_total desc")
                    ->take(8)
                    ->get();
        $data= array();
        foreach ($lives as $key => $live) {
            if(!isset($live->id)){
                continue; 
            }
            $liveModel = $this->getLiveObject($live->id,$live);
            if ($liveModel) {
                $data[] = $liveModel;
            }
        }
        return $data;
    }
    
    public function getFavoriteList(){
        $userId = $this->request->input('userId');
        $page = $this->request->input('page');
        $size = $this->request->input('size');
        //取缓存
        $liveListCache = new BaseCache();
        $liveListCache->method = 'get';
        $liveListCache->cacheName = 'getFavoriteList';
        $liveListCache->cachePar = array($userId); //集合
        $retliveListCache = $liveListCache->HandleCache();
        if ($retliveListCache === 'NO_CACHE' || ( isset($retliveListCache['more']) && $retliveListCache['more'] == 1 && count($retliveListCache['data']) <= ($info['pageSize'] * ($info['page'] + 1)) )) {
            // 1、不存在缓存 2、有缓存但缓存数量不够且数据库里仍有多个数据
//          判断页码数目
            $skip = 0;
            if ($retliveListCache !== 'NO_CACHE') {
                $skip = $retliveListCache['num'];
            }
            $take_ready = floor(($size * ($page + 1)) / $this->TAKE_NUM) + 1;
            $take = $take_ready - $skip;
            
            $lives = Live::select('live.id', 'live.title', 'live.description', 'live.live_poster', 'live.keywords', 'live.status', 
                    'live.push_stream', 'live.pull_stream', 'live.live_review', 'live.start_time', 'live.visit_total','live.praise_total','live.order_total',
                    'live.set_praise_num','live.set_visit_num','live.anchor_id','live.source_id','live.belong_to','city','live.comment_enable','live.res_id','live.is_delete')
                    ->where('live.is_delete','=','0')
                    ->where('live.status', '!=', '4')
                    ->join('favorite','favorite.live_id','=','live.id')
                    ->where('favorite.is_delete', '=', '0')
                    ->where('favorite.user_id', '=', $userId)
                    ->orderBy('favorite.created_at', 'desc')
                    ->skip($skip * $this->TAKE_NUM)
                    ->take($take * $this->TAKE_NUM)
                    ->get();
            $retLiveListCacheNew = array();
            foreach ($lives as $key => $live) {
                //存缓存-commentModel
                $liveModelCache = new BaseCache();
                $liveModelCache->method = 'put';
                $liveModelCache->cacheName = 'liveModel';
                $liveModelCache->cachePar = array($live->id); //集合
                $liveModelCache->cacheValue = $live;
                $liveModelCache->HandleCache();
                $retLiveListCacheNew['data'][]['id'] = $live->id;
            }
            if (count($lives) < $take * $this->TAKE_NUM) {
                $retLiveListCacheNew['more'] = 0;
            } else {
                $retLiveListCacheNew['more'] = 1;
            }
            $retLiveListCacheNew['num'] = $skip + ceil(count($lives) / $this->TAKE_NUM);
//            Log::info('$retLiveListCacheNew more' . $retLiveListCacheNew['more']);
//            Log::info('$retLiveListCacheNew $ake_ready' . $take_ready);
//            Log::info('$retLiveListCacheNew $num' . $retLiveListCacheNew['num']);
            if ($retliveListCache !== 'NO_CACHE') {
                $retLiveListCacheNew['data'] = array_merge((array) $retliveListCache['data'], (array) $retLiveListCacheNew['data']);
            }
            $retliveListCache =  $retLiveListCacheNew ;
            //存缓存
            $liveListCache = new BaseCache();
            $liveListCache->method = 'put';
            $liveListCache->cacheName = 'getFavoriteList';
            $liveListCache->cachePar = array($userId); //集合
            $liveListCache->cacheValue = $retliveListCache;
            $liveListCache->HandleCache();
        }
        
        //遍历缓存数组 
        $index = $size * $page;
        $liveIdList = array();
        for ($i = 0; $i < $size; $i++) {
            if (!isset($retliveListCache['data'][$index])) {
                break;
            }
            $liveIdList[] = $retliveListCache['data'][$index]['id'];
            $index++;
        }
        $data['paging']['size'] = $i;
        
        //判断是否有缓存 实体
        $liveModelCache = new BaseCache();
        $liveModelCache->method = 'has';
        $liveModelCache->cacheName = 'liveModel';
        $index_ids = array();
        foreach ($liveIdList as $key => $value) {
             $liveModelCache->cachePar = array($value); //集合
            if ($liveModelCache->HandleCache() === 'NO_CACHE') {
                if (!$value) {
                    continue;
                }
                $index_ids[] = $value;
            }
        }
        if (count($index_ids) > 0) {
            //搜索
            $lives = Live::select('live.id', 'live.title', 'live.description', 'live.live_poster', 'live.keywords', 'live.status', 
                    'live.push_stream', 'live.pull_stream', 'live.live_review', 'live.start_time', 'live.visit_total','live.praise_total','live.order_total',
                    'live.set_praise_num','live.set_visit_num','live.anchor_id','live.source_id','live.belong_to','city','live.comment_enable','live.res_id','live.is_delete')
                    ->whereIn('live.id', $index_ids)
                    ->where('live.is_delete', '=', '0')
                    ->where('live.status', '!=', '4')
                    ->get();
            foreach ($index_ids as $index_id) {
                $live_id = 0;
                foreach ($lives as $key => $live) {
                    if ($index_id == $live->id) {
                        $live_id = $index_id;
                        break;
                    }
                }
                $liveModelCache = new BaseCache();
                $liveModelCache->method = 'put';
                $liveModelCache->cacheName = 'liveModel';
                $liveModelCache->cachePar = array($live_id); //集合
                if ($live_id == 0) {
                    $liveModelCache->cacheValue = 'CACHE_BREAKDOWN';
                } else {
                    $liveModelCache->cacheValue = $live;
                }
                $liveModelCache->HandleCache();
            }
        }
        $data['data'] = array();
        //找出commentObject
        foreach ($liveIdList as $key => $id) {
            $liveModel = $this->getLiveObject($id);
            if ($liveModel) {
                $data['data'][] = $liveModel;
            }
        }
        return $data;
    }
    
    public function saveModel(){
        $userModelCache = new BaseCache();
        $userModelCache->method = 'forget';
        $userModelCache->cacheName = 'liveModel';
        $userModelCache->cachePar = array($this->id); //集合
        $userModelCache->HandleCache();
    }
}